<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>结算区域管理</title>
    <style type="text/css">
        .tableselect{
            cursor: pointer;
        }
        .tableselect:hover,.tableselect:focus{
            background: #d5d5d6;
        }
    </style>
</head>
<body>
<div ng-app="SettlementRegionApp" ng-controller="SettlementRegionCtrl">
    <div class="row">
        <ui-container url="rule/settlementregion/lists" key="rule-settlementregion-list-00">
            <ui-search-form>
                <search-region  class="col-md-6" label="请选择省份" p-name="toProvince" c-name="toCity" a-name="toCounty"></search-region>
            </ui-search-form>
            <ui-table-tool >
                <a class="btn btn-sm btn-primary" href="javascript:void(0);" ng-click="saveSettlement()" >保存</a>
            </ui-table-tool>
            <ui-table dischecked=true hasOperations=false > </ui-table>
            <table-page/>
        </ui-container>
    </div>
</div>
<script>
    angular.module('SettlementRegionApp',['ui.table']);
    angular.module('epcui', [ 'ngSanitize']);
    angular.module('SettlementRegionApp').controller('SettlementRegionCtrl', ['$scope', '$sce',function($scope,$sce){
        $scope.saveSettlement =function () {
            var param =[];
            $("select[id^='settlementLevel_']").each(function(){
                var $this = $(this);
                param.push({
                    'settlementId':$this.data("settlementid"),
                    'toCode':$this.data("tocode"),
                    'settlementLevel':$this.val()
                });
            });
            if(param.length<=0){
                toastr.error("区域类型为空，不可保存")
                return;
            }
            var convertObj = function(obj){
                var istr = "{ ";
                var p =[];
                for(var item in obj){
                    p.push("\""+item+"\":\""+obj[item]+"\"")
                }
                istr += p.join(",");
                istr += " }";
                return istr;
            };
            var paramstr ="[";
            for(var i=0;i< param.length;i++){
                if (i==0){
                    paramstr += convertObj(param[i]);
                } else {
                    paramstr += ","+ convertObj(param[i]);
                }
            }
            paramstr +="]";
            $.ajax({
                url:'rule/settlementregion/updateSettlement',
                method:"post",
                data:{settlementLevel:paramstr}
            }).done(function (result) {
                if(result.code == '0000'){
                    toastr.success("保存成功！");
                }else{
                    toastr.error(result.msg);
                }
            }).fail(function (r) {
                toastr.error("服务器异常"+r);
            });
        }
        $scope._columns=[{name:'id',head:'序号',type:'text'},
            {name:'provinceName',head:'省份',type:'text',render(data,value){
                return value ? value :'';
            }},
            {name:'cityName',head:'城市',type:'text',render(data,value){
                return value ? value :'';
            }},
            {name:'countyName',head:'区县',type:'text',render(data,value){
                return value ? value :'';
            }},
            {name:'settlementLevel',head:'区域类型',type:'select',render(data,value){
                var to_code = data.regionLevel == 1 ? data.provinceCode : (data.regionLevel == 2 ? data.cityCode : data.countyCode);
                var select =[
                    "<select id='settlementLevel_",data.settlementId,"' data-settlementid='",data.settlementId,"' data-tocode='",to_code,"'  class='tableselect' >",
                    "<option value='0' ", data.settlementLevel == 0 ? "selected" : "" ," >一类区域</option>",
                    "<option value='1' ", data.settlementLevel == 1 ? "selected" : ""  ," >二类区域</option>",
                    "<option value='2' ", data.settlementLevel == 2 ? "selected" : ""  ," >三类区域</option>",
                    "<option value='' ", (!data.settlementLevel && data.settlementLevel != 0) ? "selected" : ""  ," >无此线路</option>",
                    "</select>"
                ].join('');
                return $sce.trustAsHtml(select);
            }}
        ];
    }]);
</script>
</body>
</html>
